package com.snowflake.ecatalog.service.profile;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springside.modules.persistence.DynamicSpecifications;
import org.springside.modules.persistence.SearchFilter;
import org.springside.modules.persistence.SearchFilter.Operator;

import com.snowflake.ecatalog.contant.Status;
import com.snowflake.ecatalog.entity.GoodsCategory;
import com.snowflake.ecatalog.repository.GoodsCategoryDao;
import com.snowflake.ecatalog.util.LoginUtil;

@Service
@Transactional(readOnly = true)
public class GoodsCategoryService {
	@Autowired
	private GoodsCategoryDao goodsCategoryDao;

	public GoodsCategory getGoodsCategory(Long id) {
		return goodsCategoryDao.findOne(id);
	}

	public List<GoodsCategory> getAllGoodsCategory() {
		return (List<GoodsCategory>) goodsCategoryDao.findAll();
	}

	public List<GoodsCategory> getEnabledGoodsCategory(Long profileId) {
		return goodsCategoryDao.findByProfileIdAndStatus(profileId,Status.ENABLE);
	}

	@Transactional(readOnly = false)
	public void saveGoodsCategory(GoodsCategory newGoodsCategory) {
		newGoodsCategory.setCreator(LoginUtil.getCurrentUser());
		newGoodsCategory.setCreatedTime(new Date());
		newGoodsCategory.setStatus(Status.ENABLE);
		goodsCategoryDao.save(newGoodsCategory);
	}

	@Transactional(readOnly = false)
	public void updateGoodsCategory(GoodsCategory goodsCategory) {
		goodsCategory.setLastModifier(LoginUtil.getCurrentUser());
		goodsCategory.setLastModifiedTime(new Date());
		goodsCategoryDao.save(goodsCategory);
	}

	@Transactional(readOnly = false)
	public void deleteGoodsCategory(Long id) {
		goodsCategoryDao.delete(id);
	}

	public boolean existGoodsInCategory(Long categoryId) {
		GoodsCategory category = goodsCategoryDao.findOne(categoryId);
		return !CollectionUtils.isEmpty(category.getGoods());
	}

	@Transactional(readOnly = false)
	public void enableGoodsCategory(Long id) {
		goodsCategoryDao.changeStatus(Status.ENABLE, id);
	}

	@Transactional(readOnly = false)
	public void disableGoodsCategory(Long id) {
		goodsCategoryDao.changeStatus(Status.DISABLE, id);
	}

	public Page<GoodsCategory> getGoodsCategorys(Long profileId, Map<String, Object> filterParams, int pageNumber,
			int pageSize) {
		PageRequest pageRequest = buildPageRequest(pageNumber, pageSize);
		Specification<GoodsCategory> spec = buildSpecification(profileId, filterParams);
		return goodsCategoryDao.findAll(spec, pageRequest);
	}

	/**
	 * 创建分页请求.
	 */
	private PageRequest buildPageRequest(int pageNumber, int pagzSize) {
		return new PageRequest(pageNumber - 1, pagzSize);
	}

	/**
	 * 创建动态查询条件组合.
	 */
	private Specification<GoodsCategory> buildSpecification(Long profileId, Map<String, Object> filterParams) {
		Map<String, SearchFilter> filters = SearchFilter.parse(filterParams);
		filters.put("profile.id", new SearchFilter("profile.id", Operator.EQ, profileId));
		Specification<GoodsCategory> spec = DynamicSpecifications.bySearchFilter(filters.values(), GoodsCategory.class);
		return spec;
	}

}
